package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.BrandSeriesInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.mvp.car.CarBrandSaleListCovenant;
import com.chexiehui.jc.mvp.car.CarBrandSaleListPresenter;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.widget.LoadingLayout;
import com.chexiehui.jc.widget.indexbar.suspension.SuspensionDecoration;
import com.nex3z.flowlayout.FlowLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 新车在售列表
 */
public class CarBrandSaleListActivity extends BaseMvpActivity<CarBrandSaleListPresenter> implements CarBrandSaleListCovenant.View, View.OnClickListener {

    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    @BindView(R.id.iv_brand_image)
    ImageView ivBrandImage;
    @BindView(R.id.tv_brand_name)
    TextView tvBrandName;
    @BindView(R.id.tv_brand_introduce)
    TextView tvBrandIntroduce;
    @BindView(R.id.fl_follow_tip)
    FlowLayout flFollowTip;

    private BrandInfo brandInfo;
    private List<DictInfo> carTypes = new ArrayList<>();
    private List<CarSeriesInfo> saleListData;
    private CommonAdapter<CarSeriesInfo> saleListAdapter;
    private SuspensionDecoration suspensionDecoration;
    private int carTypePosition = -1;
    private String carType = "";
    private String energy = "";
    private int pageType;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            brandInfo = (BrandInfo) bundle.getSerializable("entity");
            energy = bundle.getString("energy");
            pageType = bundle.getInt("pageType", 0);
        }
        if (brandInfo == null) {
            brandInfo = new BrandInfo();
        }
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("在售");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_car_brand_sale_list;
    }

    @Override
    protected void initView(Bundle var1) {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableOverScrollDrag(true);
        refreshLayout.setDragRate(0.5f);
        GlideUtil.loadImageViewCrop(mContext, brandInfo.getImg(), ivBrandImage,
                R.drawable.shape_square, R.drawable.shape_square);
        tvBrandName.setText(brandInfo.getName());
        mvpPresenter.getBrandSeriesInfo();
        loadingLayout.setStatus(LoadingLayout.Loading);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        saleListData = new ArrayList<>();
        saleListAdapter = new CommonAdapter<CarSeriesInfo>(mContext, R.layout.item_choose_car_mode_seariesl, saleListData) {
            @Override
            protected void convert(ViewHolder holder, CarSeriesInfo item, int position) {
                GlideUtil.loadImageViewCrop(mContext, item.getImg(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_square, R.drawable.shape_square);
                holder.setText(R.id.tv_item_name, item.getName());
                holder.setText(R.id.tv_item_guidance_price, item.getGuidancePriceMaxMinStr());
            }
        };
        saleListAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (NoDoubleClickUtils.isDoubleClick()) return;
                if (pageType == 0) {
                    startActivity(CarSeriesDetailsActivity.class,
                            new BundleBuilder().putString("seriesId", saleListData.get(position).getId())
                                    .create());
                } else {
                    startActivity(CarSeriesForumListActivity.class,
                            new BundleBuilder().putString("seriesId", saleListData.get(position).getId())
                                    .create());
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecycler.setAdapter(saleListAdapter);
        suspensionDecoration = new SuspensionDecoration(mContext, saleListData);
        mRecycler.addItemDecoration(suspensionDecoration);
        loadingLayout.setOnReloadListener(v -> {
            if (brandInfo != null) {
                loadingLayout.setStatus(LoadingLayout.Loading);
                mvpPresenter.getBrandSeriesInfo();
            } else {
                loadingLayout.setStatus(LoadingLayout.Empty);
                loadingLayout.setEmptyText("没有找到任何数据");
            }
        });
        if (brandInfo == null) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
            return;
        }
    }

    @OnClick(R.id.tv_brand_introduce)
    public void onViewClicked() {
    }

    @Override
    protected CarBrandSaleListPresenter createPresenter() {
        return new CarBrandSaleListPresenter(this);
    }

    @Override
    public void onGetBrandSeriesInfoSuccess(BaseModel<BrandSeriesInfo> successResult) {
        if (loadingLayout.getStatus() != LoadingLayout.Success) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (carTypes.size() <= 0) {
            carTypes.addAll(successResult.getData().getCarTypeList());
            setCarTypeFlowTag(0);
        }
        saleListData.clear();
        if (successResult.getData().getSeriesGroup().size() <= 0) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
        } else {
            for (int i = 0; i < successResult.getData().getSeriesGroup().size(); i++) {
                for (int j = 0; j < successResult.getData().getSeriesGroup().get(i).getSeriesList().size(); j++) {
                    successResult.getData().getSeriesGroup().get(i).getSeriesList().get(j)
                            .setTypeName(successResult.getData().getSeriesGroup().get(i).getTypeName());
                }
                saleListData.addAll(successResult.getData().getSeriesGroup().get(i).getSeriesList());
            }
        }
        saleListAdapter.notifyDataSetChanged();
        suspensionDecoration.setmDatas(saleListData);
    }

    private void setCarTypeFlowTag(int position) {
        if (carTypePosition != position) {
            carTypePosition = position;
            carType = carTypes.get(position).getKey();
            flFollowTip.removeAllViews();
            for (int i = 0; i < carTypes.size(); i++) {
                RadiusTextView radiusTextView = (RadiusTextView) View.inflate(mContext, R.layout.item_only_car_type, null);
                radiusTextView.setTag(i);
                radiusTextView.setText(carTypes.get(i).getValue());
                radiusTextView.setOnClickListener(this);
                radiusTextView.setSelected(carTypePosition == i);
                flFollowTip.addView(radiusTextView);
            }
        }
    }

    @Override
    public void onGetBrandSeriesInfoFailure(BaseModel<Object> failureResult) {
        if (carTypes.size() <= 0) {
            if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
                loadingLayout.setStatus(LoadingLayout.Empty);
                loadingLayout.setEmptyText(failureResult.getResultInfo());
            } else {
                loadingLayout.setStatus(LoadingLayout.Error);
                loadingLayout.setErrorText(failureResult.getResultInfo());
            }
        }
    }

    @Override
    public String getBrandId() {
        return brandInfo.getId();
    }

    @Override
    public String getCarType() {
        return carType;
    }

    @Override
    public String getEnergy() {
        return energy == null ? "" : energy;
    }

    @Override
    public void onClick(View v) {
        Integer position = (Integer) v.getTag();
        setCarTypeFlowTag(position);
        mvpPresenter.getBrandSeriesInfo();
    }
}
